package com.open.capacity.finance.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.finance.dao.AuditModelDao;
import com.open.capacity.finance.dao.AuditModelWarnDao;
import com.open.capacity.finance.entity.AuditModel;
import com.open.capacity.finance.entity.AuditModelWarn;
import com.open.capacity.finance.util.uuid.UUIDUtils;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.model.system.SysUser;
import com.open.capacity.utils.DateUtil;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.SysUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.AuditModelWarnDeal;
import com.open.capacity.finance.dao.AuditModelWarnDealDao;
import com.open.capacity.finance.service.AuditModelWarnDealService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class AuditModelWarnDealServiceImpl implements AuditModelWarnDealService {

    @Autowired
    private AuditModelWarnDealDao auditModelWarnDealDao;

    @Autowired
    private AuditModelDao auditModelDao;

    @Autowired
    private AuditModelWarnDao auditModelWarnDao;

    /**
     * 添加
     *
     * @param auditModelWarnDeal
     */
    @Transactional
    public int save(AuditModelWarnDeal auditModelWarnDeal) {
        SysUser loginUser = SysUserUtil.getLoginAppUser();
        if (StringUtil.isEmpty(auditModelWarnDeal.getId())) {
            auditModelWarnDeal.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        }
        auditModelWarnDeal.setCompanyId(loginUser.getCompanyId());
        auditModelWarnDeal.setUserId(loginUser.getId());
        auditModelWarnDeal.setUserName(loginUser.getRealName());
        auditModelWarnDeal.setCreateTime(new Date());
        auditModelWarnDealDao.save(auditModelWarnDeal);

        String[] warnArray = auditModelWarnDeal.getWarnId().split(",");
        String[] modelArray = auditModelWarnDeal.getModelId().split(",");
        for (int i = 0; i < warnArray.length; i++) {
            AuditModel model = auditModelDao.findById(modelArray[i]);
            AuditModelWarn warnModel = auditModelWarnDao.findById(Long.valueOf(warnArray[i]));
            if (model != null && warnModel != null) {
                warnModel.setDealFlag(1);
                warnModel.setDealEnddate(DateUtil.addDay(new Date(), model.getRectifyPeriod()));
                auditModelWarnDao.update(warnModel);
            }
        }

        return 1;
    }

    /**
     * 修改
     *
     * @param auditModelWarnDeal
     */
    public int update(AuditModelWarnDeal auditModelWarnDeal) {
        return auditModelWarnDealDao.update(auditModelWarnDeal);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return auditModelWarnDealDao.delete(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result auth(String id, String description) throws Exception {
        AuditModelWarnDeal old = auditModelWarnDealDao.findById(id);
        if (old == null) {
            return Result.failed("记录不存在！");
        }

        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        String[] warnIdArray = old.getWarnId().split(",");
        for (String warnId : warnIdArray) {
            AuditModelWarn warn = auditModelWarnDao.findById(Long.valueOf(warnId));
            warn.setDealFlag(2);
            auditModelWarnDao.updateDealFlag(Long.valueOf(warnId), 2);
        }

        return Result.succeed("审核成功");
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public AuditModelWarnDeal findById(String id) {
        return auditModelWarnDealDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<AuditModelWarnDeal> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<AuditModelWarnDeal> list = auditModelWarnDealDao.findAll(params);
        PageInfo<AuditModelWarnDeal> pageInfo = new PageInfo(list);

        return PageResult.<AuditModelWarnDeal>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<AuditModelWarnDeal> findAll(Map<String, Object> params) {
        return auditModelWarnDealDao.findAll(params);
    }

}
